package com.sunlands.zlcx.usercenter.controller;

import com.sunlands.zlcx.usercenter.common.annotation.AutoLog;
import com.sunlands.zlcx.usercenter.constant.dic.ADVERT;
import com.sunlands.zlcx.usercenter.domain.BuryingPointDO;
import com.sunlands.zlcx.usercenter.domain.DicDO;
import com.sunlands.zlcx.usercenter.exception.GlobalExceptionHandler;
import com.sunlands.zlcx.usercenter.service.BuryingPointServiceImpl;
import com.sunlands.zlcx.usercenter.service.DicServiceImpl;
import com.sunlands.zlcx.usercenter.vo.response.BusinessResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

import static com.sunlands.zlcx.usercenter.common.annotation.AutoLog.Level.DEBUG;

/**
 * @author anjl
 * @date 1:35 PM 2019/4/15
 * description:
 */
@SuppressWarnings("unused")
@Api(tags = "12、埋点信息", position = 12)
@ApiResponses(value = {@ApiResponse(code = 400, response = GlobalExceptionHandler.class, message = "数据校验失败"), @ApiResponse(code = 500, response = GlobalExceptionHandler.class, message = "内部错误")})
@Slf4j
@Configuration
@RestController
@RequestMapping("/burying-point")
public class BuryingPointController implements AuthSupport {

    @Autowired
    private BuryingPointServiceImpl buryingPointService;

    @Autowired
    private DicServiceImpl dicService;

    @ApiOperation(value = "埋点APP调用")
    @AutoLog
    @PostMapping("/add")
    public BusinessResult<BuryingPointDO> add(@RequestBody BuryingPointDO buryingPointDO) {

        Asserts.notEmpty(buryingPointDO.getModular(), "modular不能为空！");
        Asserts.notEmpty(buryingPointDO.getFunction(), "function不能为空！");
        Asserts.notEmpty(buryingPointDO.getOperationType(), "operationType不能为空！");
        Asserts.notEmpty(buryingPointDO.getPointSite(), "pointSite不能为空！");

        Long userId = currentUserId();
        if (Objects.isNull(userId)) {
            return BusinessResult.createSuccessInstance(buryingPointDO);
        }

        buryingPointDO.setUserId(userId);
        DicDO dic = dicService.findByCode(ADVERT.dicName());
//        if (YesNoStatus.NO.getCode() == dic.filter(ADVERT.ios_switch.name()).getCode()
//                && YesNoStatus.NO.getCode() == dic.filter(ADVERT.android_switch.name()).getCode()) {
//            return BusinessResult.createSuccessInstance(buryingPointDO);
//        }

        commonTaskExecutor.submit(() -> buryingPointService.add(buryingPointDO));
        return BusinessResult.createSuccessInstance(buryingPointDO);
    }

    @ApiOperation(value = "埋点-服务调用调用")
    @AutoLog(logLevel = DEBUG)
    @PostMapping("/client/add")
    public BusinessResult<BuryingPointDO> addByClient(@RequestBody BuryingPointDO buryingPointDO) {

        Asserts.notEmpty(buryingPointDO.getModular(), "modular不能为空！");
        Asserts.notEmpty(buryingPointDO.getFunction(), "function不能为空！");
        Asserts.notEmpty(buryingPointDO.getFunction(), "function不能为空！");
        Asserts.notEmpty(buryingPointDO.getPointSite(), "pointSite不能为空！");
        Asserts.notNull(buryingPointDO.getUserId(), "userId不能为空！");

        commonTaskExecutor.submit(() -> buryingPointService.add(buryingPointDO));
        return BusinessResult.createSuccessInstance(buryingPointDO);
    }

    @Resource(name = "commonTaskExecutor")
    private AsyncTaskExecutor commonTaskExecutor;

}
